#!/bin/sh

######################################################
#                                                    #
#     SVN冲突一键解决神器: OneKeyAutoMerge              #
#     Author: xudong.jxd@alibaba-inc.com 承旭         #
#     Version: 1.0                                   #
#                                                    #
######################################################



# -----------   设置项 settings   ----------
# 1.冲突文件夹目录
conflictsDirectory="/Users/JXD/Desktop/Ali/WORK/conflicts/"

# w.aone链接前缀, 一般不用修改
aoneUrl="http://svn.taurus.taobao.net/repos/conflict/aone2/"

# 编辑器脚本需要在下文中设置，请关注注释(2处)

# ----------- 设置项 settings end ----------





startTime=$((`date +%s`))

# 1. 处理用户输入，并做容错处理
#ex1: 2016-06-25_16-17-59_10708018
#ex2: /2016-06-25_16-17-59_10708018
#ex3: ict/aone2/2016-06-27_22-38-34_10744193
branch=$1
split="/20"
pos=${branch/${split}*/}
position=$((${#pos} + 1))
length=${#branch}
if (($position < $length)); then
	branch=${branch:$position}
fi
if (($length < 5)); then
	echo "NOT A BRAUNCH!!!"
	exit 1
fi
svnUrl=$aoneUrl$branch
echo "\nvvvvvvvvvvvv OneKeyMerge vvvvvvvvvvvv"
echo "\t Work:   Starting"
echo "\t Branch: $branch"
echo "vvvvvvvvvvvv OneKeyMerge vvvvvvvvvvvv\n"

# 2. 进入目录并co, 然后进入分支
originDir=$(pwd)
cd "$conflictsDirectory"
pwd
echo svn co $svnUrl
if [ 'nco' == $2 ]; then
	echo ''
else
	svn co $svnUrl
fi
cd "$conflictsDirectory$branch"

# 3. 输出冲突列表, 并使用编辑器打开文件
fileListStr=$(grep -lr ".working" *)
fileList=($fileListStr)
fileNum=${#fileList[@]}
if (($fileNum > 0)); then
	echo "\n\033[31m$fileNum conflicts founded: \033[0m"
fi
openEditor="false"
for file in $fileListStr; do
	openEditor="true"
	echo "$file"
done

fileListStr=$fileListStr" "$(grep -lr ">>>>>>>" *)
fileListStr=$fileListStr" "$(grep -lr "<<<<<<<" *)
if [ "true" == $openEditor ]; then
	########## !!! 在此设置编辑器脚本 !!! ##########
	/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl -n -a $conflictsDirectory$branch $fileListStr
	########## !!! 在此设置编辑器脚本 !!! ##########
else
	echo "nothing to merge."
fi


# 4. 开始提交流程
merged="false"
interrupted="false"
while [ "false" == $merged ]; do
	echo "\n\033[32mpress RETURN to start ci: \033[0m"
	read;[[ -z $REPLY ]] && echo "" || interrupted="true"
	if [ "true" == $interrupted ]; then
		echo "Interrupted by user."
		exit 1
	fi
	# 检查merge情况
	echo "check merging status..."

	merged="true"
	xFileListStr=$(grep -lr ".working" *)
	xFileListStr=$xFileListStr" "$(grep -lr ">>>>>>>" *)
	xFileListStr=$xFileListStr" "$(grep -lr "<<<<<<<" *)
	for file in $xFileListStr; do
		if [ "true" == $merged ]; then
			merged="false"
			echo "\n\033[31m==========!!! Files unMergerd !!!==========\033[0m"
		fi
		echo "$file"
	done

	if [ "false" == $merged ]; then
		echo "\ncopy bellow if you DO want to ci:"
		echo "cd $conflictsDirectory$branch && svn ci -m \"oneKeyAutoMerge\" && cd $originDir"
		echo "\neditor will open in 5 seconds..."
		sleep 5
		########## !!! 在此设置编辑器脚本 !!! ##########
		/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl -n -a $conflictsDirectory$branch $xFileListStr
		########## !!! 在此设置编辑器脚本 !!! ##########
	fi

done

echo "ok"
svn status
svn ci -m "OneKeyAutoMerge"

endTime=$((`date +%s`))
timeCost=$(($endTime-$startTime))
echo "\n^^^^^^^^^^^^ OneKeyMerge ^^^^^^^^^^^^"
echo "\t Work: DONE"
echo "\t Time: $timeCost seconds"
echo "^^^^^^^^^^^^ OneKeyMerge ^^^^^^^^^^^^"

sleep 0.5
open /Applications/Google\ Chrome.app
